package me.shurufa.widget.timelineheader;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import me.shurufa.R;
import me.shurufa.utils.Utils;
import me.shurufa.widget.animutils.AnimOnTouchListener;

/**
 * 作者 : andy
 * 日期 : 16/3/10 10:04
 * 邮箱 : andyxialm@gmail.com
 * 描述 : 书摘Bar
 */
public class HeaderDigestBarLayout extends LinearLayout implements View.OnClickListener {

    public static final int INDEX_PRAISE  = 0;
    public static final int INDEX_CHAPTER = 1;
    public static final int INDEX_TIME    = 2;
    public static final int INDEX_PAGE    = 3;
    public static final int INDEX_ME      = 4;

    private LinearLayout ll_praise;
    private LinearLayout ll_chapter;
    private LinearLayout ll_time;
    private LinearLayout ll_page;
    private LinearLayout ll_me;

    private TextView tv_praise;
    private TextView tv_praise_num;
    private TextView tv_chapter;
    private TextView tv_time;
    private TextView tv_page;
    private TextView tv_me;

    private ImageView iv_sort_time;
    private ImageView iv_sort_page;

    // 存储状态
    private SparseBooleanArray mBoolArray = new SparseBooleanArray(5); // true: ASC    false: DESC
    private int mSelectedIndex = INDEX_PRAISE; // 默认选中最赞
    private OnBarItemClickListener mListener;

    public HeaderDigestBarLayout(Context context) {
        this(context, null);
    }

    public HeaderDigestBarLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeaderDigestBarLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public HeaderDigestBarLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        setOrientation(VERTICAL);
        View contentView = View.inflate(getContext(), R.layout.header_digest_bar, null);

        ll_praise = (LinearLayout) contentView.findViewById(R.id.ll_praise);
        ll_chapter = (LinearLayout) contentView.findViewById(R.id.ll_chapter);
        ll_time = (LinearLayout) contentView.findViewById(R.id.ll_time);
        ll_page = (LinearLayout) contentView.findViewById(R.id.ll_page);
        ll_me = (LinearLayout) contentView.findViewById(R.id.ll_me);

        tv_praise = (TextView) contentView.findViewById(R.id.tv_praise);
        tv_praise_num = (TextView) contentView.findViewById(R.id.tv_praise_num);
        tv_chapter = (TextView) contentView.findViewById(R.id.tv_chapter);
        tv_time = (TextView) contentView.findViewById(R.id.tv_time);
        tv_page = (TextView) contentView.findViewById(R.id.tv_page);
        tv_me = (TextView) contentView.findViewById(R.id.tv_me);

        iv_sort_time = (ImageView) contentView.findViewById(R.id.iv_sort_time);
        iv_sort_page = (ImageView) contentView.findViewById(R.id.iv_sort_page);

        ll_praise.setOnClickListener(this);
        ll_chapter.setOnClickListener(this);
        ll_time.setOnClickListener(this);
        ll_page.setOnClickListener(this);
        ll_me.setOnClickListener(this);

        ll_praise.setOnTouchListener(new AnimOnTouchListener());
        ll_chapter.setOnTouchListener(new AnimOnTouchListener());
        ll_time.setOnTouchListener(new AnimOnTouchListener());
        ll_page.setOnTouchListener(new AnimOnTouchListener());
        ll_me.setOnTouchListener(new AnimOnTouchListener());
        addView(contentView);

        // 设置升序降序布尔值
        mBoolArray.put(INDEX_PRAISE, false);
        mBoolArray.put(INDEX_CHAPTER, true);
        mBoolArray.put(INDEX_TIME, false);
        mBoolArray.put(INDEX_PAGE, true);
        mBoolArray.put(INDEX_ME, false);

        // 默认选中第一项
        setSelected(INDEX_PRAISE);
    }

    private void changeTextStyleBySate(int selectedIndex) {
        mSelectedIndex = selectedIndex;
        int selectedColor = getContext().getResources().getColor(R.color.red_tab_text);
        int unSelectedColor = getContext().getResources().getColor(R.color.gray_tab_text);
        tv_praise.setTextColor(selectedIndex == INDEX_PRAISE ? selectedColor : unSelectedColor);
        tv_chapter.setTextColor(selectedIndex == INDEX_CHAPTER ? selectedColor : unSelectedColor);
        tv_time.setTextColor(selectedIndex == INDEX_TIME ? selectedColor : unSelectedColor);
        tv_page.setTextColor(selectedIndex == INDEX_PAGE ? selectedColor : unSelectedColor);
        tv_me.setTextColor(selectedIndex == INDEX_ME ? selectedColor : unSelectedColor);

        boolean timeSort = mBoolArray.get(INDEX_TIME);
        iv_sort_time.setImageResource(INDEX_TIME == selectedIndex
                ? (timeSort ? R.drawable.ic_sort_asc : R.drawable.ic_sort_desc)
                : R.drawable.ic_sort);

        boolean pageSort = mBoolArray.get(INDEX_PAGE);
        iv_sort_page.setImageResource(INDEX_PAGE == selectedIndex
                ? (pageSort ? R.drawable.ic_sort_asc : R.drawable.ic_sort_desc)
                : R.drawable.ic_sort);
    }

    /**
     * 获得当前选中的索引
     * @return
     */
    public int getCurrentIndex() {
        return mSelectedIndex;
    }


    /**
     * 获得当前索引 的排列方式
     * @return DESC = true  ASC: false
     */
    public boolean isDescOfCurrentItem() {
        return !mBoolArray.get(getCurrentIndex());
    }

    /**
     * 设置默认选中
     * @param index
     */
    public void setSelected(int index) {
        changeTextStyleBySate(index);
        if (mListener != null) {
            mListener.onClick(index, true, mSelectedIndex);
        }
    }

    /**
     * 隐藏自己
     */
    public void hideMe() {
        ll_me.setVisibility(GONE);
    }

    public void setOnBarItemClickListener(OnBarItemClickListener l) {
        this.mListener = l;
    }

    @Override
    public void onClick(View v) {

        if (Utils.isFastClick(500)) {
            Utils.showToast(R.string.click_too_fast);
            return;
        }

        if (mListener == null) {
            return;
        }

        // int curSelectedIndex = 0;
        switch (v.getId()) {
            case R.id.ll_praise:
                mSelectedIndex = INDEX_PRAISE;
                mListener.onClick(INDEX_PRAISE, true, mSelectedIndex);
                break;


            case R.id.ll_chapter:
                mSelectedIndex = INDEX_CHAPTER;
                mListener.onClick(INDEX_CHAPTER, false, mSelectedIndex);
                break;


            case R.id.ll_time:
                boolean newTimeState = false;  // 默认降序
                if (getCurrentIndex() == INDEX_TIME) {
                    newTimeState = !mBoolArray.get(INDEX_TIME);
                }
                mBoolArray.put(INDEX_TIME, newTimeState);
                mSelectedIndex = INDEX_TIME;
                mListener.onClick(INDEX_TIME, newTimeState, mSelectedIndex);
                break;


            case R.id.ll_page:
                boolean newPageState = true;  // 默认升序
                if (getCurrentIndex() == INDEX_PAGE) {
                    newPageState = !mBoolArray.get(INDEX_PAGE);
                }
                mBoolArray.put(INDEX_PAGE, newPageState);
                mSelectedIndex = INDEX_PAGE;
                mListener.onClick(INDEX_PAGE, newPageState, mSelectedIndex);
                break;


            case R.id.ll_me:
                mSelectedIndex = INDEX_ME;
                mListener.onClick(INDEX_ME, true, mSelectedIndex);
                break;
        }

        changeTextStyleBySate(mSelectedIndex);
    }

}
